﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ANZ_AIM_DAL;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using System.Net;



namespace ANZ_AIM_BLL
{
    public class LoggerBLL
    {
        private const string TABLE_NAME = "Log";
        private string connectionString;
        private string description;
        private Exception ex;




        public string Description
        {
            get { return description; }
            set { description = value; }
        }


        public Exception Ex
        {
            get { return ex; }
            set { ex = value; }
        }

        public LoggerBLL()
        {
            connectionString = DBHelper.GetMainMdbConnectionString();
        }

        public string GetIpAddress()
        {
            string str_Ip = "";
            try
            {
                string hostName = Dns.GetHostName();
                str_Ip = Dns.GetHostByName(hostName).AddressList[0].ToString();

            }
            catch (Exception)
            {
                str_Ip = "[Error with connection]";

            }
            return str_Ip;
        }

        public void Log(Form frm, string username)
        {
            StackFrame frame = new StackFrame(1);
            var method = frame.GetMethod();
            string strEvent = method.Name;


            description = description == null || description == string.Empty ? this.ex != null ? "Message: " + ex.Message + ex.StackTrace : "Done: No error" : description;

            OleDbParameter[] cmdParams = new OleDbParameter[6];
            cmdParams[0] = new OleDbParameter("@IpAdd", GetIpAddress());
            cmdParams[1] = new OleDbParameter("@event", strEvent);
            cmdParams[2] = new OleDbParameter("@user", username == null || username == string.Empty ? "Null" : username);
            cmdParams[3] = new OleDbParameter("@form", frm.Name + " - [" + frm.Text + "]");
            cmdParams[4] = new OleDbParameter("@time", DateTime.Parse(DateTime.Now.ToLongTimeString()));
            cmdParams[5] = new OleDbParameter("@des", description);

            string command = string.Format("Insert into [{0}]([IpAddress], [Event], [User], [Form], [CreatedDate], [Description]) values(@IpAdd, @event, @user, @form, @time, @des)", TABLE_NAME);
            try
            {
                OleDbHelper.ExecuteNonQuery(this.connectionString, CommandType.Text, command, cmdParams);
            }
            catch (OleDbException oleEx)
            {
                MessageBox.Show(oleEx.Message + "\n" + oleEx.StackTrace);

            }
        }

        public DataTable LogFilter(string username, string fromDate, string toDate)
        {
            string command = "";
            const string DEFAULT_DATE = "01/01/0001";

            if (fromDate == DEFAULT_DATE && toDate == DEFAULT_DATE)
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%' ORDER BY [CreatedDate] ASC", TABLE_NAME, username);
            }
            else if (fromDate != DEFAULT_DATE && toDate == DEFAULT_DATE)
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%'and [CreatedDate] >= #{2} 00:00:00 AM# ORDER BY [CreatedDate] ASC", TABLE_NAME, username, fromDate);
            }
            else if (fromDate == DEFAULT_DATE && toDate != DEFAULT_DATE)
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%'and [CreatedDate] <= #{2} 11:59:59 PM# ORDER BY [CreatedDate] ASC", TABLE_NAME, username, toDate);
            }
            else
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%'and [CreatedDate] >= #{2} 00:00:00 AM# And [CreatedDate] <= #{3} 11:59:59 PM# ORDER BY [CreatedDate] ASC", TABLE_NAME, username, fromDate, toDate);
            }

            DataSet ds = OleDbHelper.ExecuteDataset(this.connectionString, CommandType.Text, command);
            return ds.Tables[0];
        }


    }
}
